<?php
/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/17
 * Time: 上午11:01
 */

defined('BASEPATH') OR exit('No direct script access allowed');

class Administrator extends CI_Controller
{

    public function index()
    {
        $this->load->helper('url');
        Permission::administratorCheck();
    }

    /**
     * 任务列表
     */
    public function task()
    {
        Permission::gl();
        $data = array(
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
        );
        $this->load->view('administrator_task', $data);
    }

    /**
     * 更改领取状态
     */
    public function changeReceivedStatus()
    {
        Permission::gl(1);
        $translation_id = isset($_REQUEST['translation_id']) ? $_REQUEST['translation_id'] : 0;
        $manager_id = isset($_REQUEST['manager_id']) ? $_REQUEST['manager_id'] : 0;
        if (empty($translation_id) || empty($manager_id)) {
            exit('Error : empty information');
        }
        $this->load->model('Lyw_article_manager_model');
        if ($this->Lyw_article_manager_model->changeReceivedStatus($manager_id, $translation_id)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('Error'));
        }
    }

    /**
     * 激活码
     */
    public function code()
    {
//        Permission::gl();
        Permission::gz();
        $data = array(
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
        );
        $this->load->helper('url');
        $this->load->view('administrator_code', $data);
    }

    /**
     * 用户管理
     */
    public function user()
    {
        Permission::gl();
        $data = array('header_json' => $this->lyw_common_model->buildHeaderJson());
        $this->load->helper('url');
        $this->load->view('administrator_user', $data);
    }

    public function createCode()
    {
//        Permission::gl(1);
        Permission::gz();
        $this->load->model('Lyw_invite_code_manager_model');
        if ($this->Lyw_invite_code_manager_model->createCode()) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('error'));
        }
    }

    public function getCodeJson()
    {
//        Permission::gl(1);
        Permission::gz();
        $this->load->model('Lyw_invite_code_manager_model');
        $list = $this->Lyw_invite_code_manager_model->getAllListInfoIsVaild();
        exit(json_encode($list));
    }


    public function getTaskJson()
    {
        $page = isset($_POST['page']) ? $_POST['page'] : 0;
        $rows = isset($_POST['rows']) ? $_POST['rows'] : 5;
        $this->load->model('Lyw_article_manager_model');
        $manager_list = $this->lyw_common_model->buildTaskJson($page, $rows);
        $this->Lyw_article_manager_model->getFinishListInfo($manager_list);
        $countAll = $this->Lyw_article_manager_model->countIsNoDel();
        exit(json_encode(array('list' => $manager_list, 'count_all' => $countAll)));
    }

    public function getUserListJson()
    {
        Permission::gl(1);
        $name = isset($_POST['name']) ? $_POST['name'] : '';
        $page = isset($_POST['page']) ? $_POST['page'] : 1;
        $rows = isset($_POST['rows']) ? $_POST['rows'] : 10;
        $this->load->model('lyw_user_model');
        $result = $this->lyw_user_model->getSearchUserlist($page, $rows, $name);
        $count = $this->lyw_user_model->searchCount($name);
        exit(json_encode(array('list' => $result, 'count' => $count)));
    }

    public function stopUser()
    {
        Permission::gl(1);
        $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : '';
        if (empty($user_id)) {
            exit(json_encode('empty'));
        }
        $this->load->model('lyw_user_model');
        if ($this->lyw_user_model->stopUser($user_id)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('failure'));
        }
    }

    public function startUser()
    {
        Permission::gl(1);
        $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : '';
        if (empty($user_id)) {
            exit(json_encode('empty'));
        }
        $this->load->model('lyw_user_model');
        if ($this->lyw_user_model->startUser($user_id)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('failure'));
        }
    }

    public function deleteTask()
    {
        Permission::gl(1);
        $id = isset($_POST['id']) ? $_POST['id'] : 0;
        if (empty($id)) {
            exit(json_encode('empty'));
        }
        $this->load->model('lyw_article_manager_model');
        if ($this->lyw_article_manager_model->del($id)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('failure'));
        }
    }

    public function changeNotice()
    {
        Permission::gl(1);
        $gonggao = isset($_POST['gonggao']) ? $_POST['gonggao'] : '';
        if (empty($gonggao)) {
            exit(json_encode('empty'));
        }
        $fp = fopen(BASEPATH . '../assets/gonggao.txt', 'w+');
        fwrite($fp, $gonggao);
        fclose($fp);
        exit(json_encode('success'));
    }

    public function notice()
    {
        Permission::gl();
        $this->load->helper('url');
        Permission::administratorCheck();
        $gonggao = '公告:' . file_get_contents(BASEPATH . '../assets/gonggao.txt');
        $data = array(
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
            'gonggao' => $gonggao
        );
        $this->load->view('administrator_notice', $data);
    }

    public function translation()
    {
        Permission::gl();
        $this->load->view('admin_translation', $this->lyw_common_model->readTranslation());
    }

    public function proofread()
    {
        Permission::gl();
        $this->load->view('taskworker_proofead', $this->lyw_common_model->readProofread());
    }

    public function upUser()
    {
        Permission::gl(1);
        $user_id = isset($_POST['user_id']) ? $_POST['user_id'] : '';
        if (empty($user_id)) {
            exit(json_encode('empty'));
        }
        $this->load->model('lyw_user_model');
        if ($this->lyw_user_model->upUser($user_id)) {
            exit(json_encode('success'));
        } else {
            exit(json_encode('用户已存在操作记录,升级失败!'));
        }
    }

    public function tiXian()
    {
        Permission::gl();
        $data = array('header_json' => $this->lyw_common_model->buildHeaderJson());
        $this->load->helper('url');
        $this->load->view('administrator_tixian', $data);
    }

    public function getTiXianJson()
    {
        Permission::gl(1);
        $type = isset($_POST['type']) ? $_POST['type'] : '';
        $this->load->model('Lyw_tixian_record_model');
        $list = $this->Lyw_tixian_record_model->getPayList($type);
        exit(json_encode($list));
    }

    public function doPay()
    {
        Permission::gl(1);
        $id = isset($_POST['id']) ? $_POST['id'] : 0;
        $this->load->model('Lyw_tixian_record_model');
        if ($this->Lyw_tixian_record_model->doPay($id)) {
            exit(json_encode(1));
        } else {
            exit(json_encode(0));
        }
    }

    public function getTiXianSum()
    {
        Permission::gl(1);
        $this->load->model('Lyw_tixian_record_model');
        $sum = $this->Lyw_tixian_record_model->getSum();
        exit(json_encode($sum));
    }

    public function getGaoFeiJson()
    {
        Permission::gl(1);
        $this->load->model('Lyw_gaofei_record_model');
        $list = $this->Lyw_gaofei_record_model->getListJson();
        foreach ($list as $k => &$v) {
            $v['creat_time'] = date('Y-m-d H:i:s', $v['creat_time']);
        }
        exit(json_encode($list));
    }

    public function getGaoFeiSum()
    {
        Permission::gl(1);
        $this->load->model('Lyw_gaofei_record_model');
        $sum = $this->Lyw_gaofei_record_model->getSum();
        exit(json_encode($sum));
    }

    public function zy_zishutongji()
    {
        Permission::gl();
        $data = array(
            'header_json' => $this->lyw_common_model->buildHeaderJson(),
        );
        $this->load->view('administrator_zy_zishutongji', $data);
    }

    public function getZYzishutongjiJson()
    {
        Permission::gl(1);
        $this->load->model('Lyw_proofread_article_model');
        $result = $this->Lyw_proofread_article_model->getZYzishutongji();
        exit(json_encode($result));
    }

    /**
     * 管理员对用户密码进行修改
     */
    public function cpasswordChange()
    {
        Permission::gl(1);
        $password1 = isset($_POST['password1']) ? $_POST['password1'] : '';
        $password2 = isset($_POST['password2']) ? $_POST['password2'] : '';
        $username = isset($_POST['username']) ? $_POST['username'] : '';

        $this->load->model('lyw_user_model');
        $list = $this->lyw_user_model->getUserInforByUsername($username);
        if(sizeof($list) > 1){
            exit(json_encode('存在重复用户名,密码修改失效!'));
        }
        if(empty($list)){
            exit(json_encode('不存在该用户,密码修改失效!'));
        }
        if($list[0]['type'] == 1){
            exit(json_encode('禁止修改普通用户密码!'));
        }
        if ($this->lyw_user_model->cPassword($username, $password1)) {
            exit(json_encode('密码修改成功!'));
        } else {
            exit(json_encode('密码修改失败!'));
        }
    }

    public function shoudong(){
        Permission::gl();
//        $this->lyw_common_model->shouDongJiSuanProofeadNumber();
        $this->lyw_common_model->shouDongJiSuanJingYan();
    }
}